#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

struct CompareList
{
    bool operator()(const int &lhs, const int &rhs) const
    {
        cout << "struct CompareList" << endl;
        return lhs < rhs;
    }
};

void test()
{
    list<int> number = {1, 3, 5, 7, 5, 5, 3, 5, 3, 8};
    display(number);

    cout << endl << "list的unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "list的元素的逆置" << endl;
    number.reverse();
    display(number);

    cout << endl << "list的sort操作" << endl;
    number.sort();
    /* std::less<int>  lst; */
    /* number.sort(lst); */
    /* number.sort(std::less<int>()); */
    /* number.sort(std::greater<int>()); */
    /* number.sort(CompareList()); */
    display(number);

    cout << endl << "list的unique函数" << endl;
    number.unique();
    display(number);

    cout << endl << "list的merge操作" << endl;
    list<int> other = {11, 55, 44, 33, 22, 6};
    other.sort();
    display(other);
    number.sort();
    display(number);
    number.merge(other);
    display(number);
    display(other);
}

void test2()
{
    list<int> number = {1, 3, 5, 9, 8};
    display(number);

    list<int> other = {11, 44, 77, 99, 33, 22};
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    /* number.splice(it, other); */
    /* display(number); */
    /* display(other); */

    auto it2 = other.end();
    --it2;
    cout << "*it2 = " << *it2 << endl;
    number.splice(it, other, it2);
    display(number);
    display(other);

    cout << endl << endl << endl;
    auto it3 = other.begin();
    cout << "*it3 = " << *it3 << endl;
    it2 = other.end();
    --it2;
    /* --it2; */
    cout << "*it2 = " << *it2 << endl;
    number.splice(it, other, it3, it2);
    display(number);
    display(other);

    cout << endl << "splice在同一个链表中进行操作" << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    it2 = number.end();
    --it2;
    --it2;
    cout << "*it2 = " << *it2 << endl;
    number.splice(it, number, it2);
    display(number);
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

